package org.eiahe.hr.interview.controller;

import lombok.extern.slf4j.Slf4j;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.eiahe.hr.interview.domain.dto.InterviewPositionCategoryQueryDTO;
import org.eiahe.hr.interview.domain.vo.InterviewPositionCategoryVO;
import org.eiahe.hr.interview.domain.dto.InterviewPositionCategoryDTO;
import org.eiahe.hr.interview.service.InterviewPositionCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


/**
 * 职位分类表
 * @author :zhangnn
 * @className :InterviewPositionCategoryController
 * @description: TODO
 * @date 2025-08-13 16:03:41
 */
@Slf4j
@RestController
@RequestMapping("/interview/category")
public class InterviewPositionCategoryController extends BaseController {
    @Autowired
    private InterviewPositionCategoryService categoryService;


    /**
     * 分页查询职位分类列表
     */
    @GetMapping("/page")
    public TableDataInfo<InterviewPositionCategoryVO> list(InterviewPositionCategoryQueryDTO queryDTO, PageQuery pageQuery) {
        TableDataInfo<InterviewPositionCategoryVO> page = categoryService.selectCategoryPage(queryDTO,pageQuery);
        return page;
    }

    /**
     * 查询所有启用的职位分类
     */
    @GetMapping("/all")
    public R<List<InterviewPositionCategoryDTO>> getAllEnabled() {
        List<InterviewPositionCategoryDTO> list = categoryService.selectAllEnabled();
        return R.ok(list);
    }

    /**
     * 根据ID查询职位分类详情
     */
    @GetMapping("/{id}")
    public R<InterviewPositionCategoryDTO> getById(@PathVariable Long id) {
        InterviewPositionCategoryDTO category = categoryService.selectById(id);
        return R.ok(category);
    }

    /**
     * 新增职位分类
     */
    @Log(title = "新增职位分类", linkType = "interview", businessType = BusinessType.INSERT, nameField = "categoryName")
    @PostMapping("/create")
    public R<Void> create(@Validated @RequestBody InterviewPositionCategoryDTO categoryDTO) {
        // 检查分类代码是否已存在
        if (categoryService.checkCategoryCodeExists(categoryDTO.getCategoryCode(), null)) {
            return R.fail("分类代码已存在");
        }

        boolean success = categoryService.insertCategory(categoryDTO);
        return success ? R.ok("新增成功") : R.fail("新增失败");
    }

    /**
     * 更新职位分类
     */
    @Log(title = "更新职位分类", linkType = "interview", businessType = BusinessType.UPDATE,  nameField = "categoryName")
    @PostMapping("/update")
    public R<Void> update(@Validated @RequestBody InterviewPositionCategoryDTO categoryDTO) {
        // 检查分类代码是否已存在（排除当前记录）
        if (categoryService.checkCategoryCodeExists(categoryDTO.getCategoryCode(), categoryDTO.getId())) {
            return R.fail("分类代码已存在");
        }

        boolean success = categoryService.updateCategory(categoryDTO);
        return success ? R.ok("更新成功") : R.fail("更新失败");
    }

    /**
     * 删除职位分类
     */
    @Log(title = "删除职位分类", linkType = "interview", businessType = BusinessType.DELETE,  nameField = "categoryName")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Long id) {
        boolean success = categoryService.deleteCategory(id);
        return success ? R.ok("删除成功") : R.fail("删除失败");
    }

    /**
     * 批量删除职位分类
     */
    @Log(title = "删除职位分类", linkType = "interview", businessType = BusinessType.DELETE,  nameField = "categoryName")
    @DeleteMapping("/batch/{ids}")
    public R<Void> batchDelete(@PathVariable String ids) {
        List<Long> idList = Arrays.stream(ids.split(","))
            .map(Long::valueOf)
            .collect(java.util.stream.Collectors.toList());

        boolean success = categoryService.deleteCategoryByIds(idList);
        return success ? R.ok("批量删除成功") : R.fail("批量删除失败");
    }
}
